package com.hdoit.ares.web.cust.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.CustPoster;
import com.hdoit.ares.logical.cust.service.ICustPosterService;
import com.hdoit.ares.logical.viewmodel.cust.CustPosterSearch;
import com.hdoit.ares.selfcommon.enums.cust.CustPosterType;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;


@Controller
@RequestMapping(value = "/admin/cust/custPoster/")
public class CustPosterController extends BaseController{

	@Override
	public String getRoutePath() {
		
		return "areas/admin/cust/custPoster/";
	}

	private ICustPosterService custPosterService;
	
	public ICustPosterService getCustPosterService() {
		return custPosterService;
	}

	@Resource
	public void setCustPosterService(ICustPosterService custPosterService) {
		this.custPosterService = custPosterService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String saveViewName = "save";
	private final String uploadPosterViewName = "uploadPoster";
	private final String deleteViewName = "delete";
	
	@RequestMapping(value = indexViewName,method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model,PagerModel pagerModel,CustPosterSearch search,boolean ajaxPost){
		List<CustPoster> custPosters = custPosterService.queryListBack(search,pagerModel);
		pagerModel.setTotalAmount(custPosters.size());
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("custPosters", custPosters);
		model.addAttribute("custPosterSearch", search);
		
		if(ajaxPost){
			return view(outputViewName); 
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value = uploadPosterViewName,method= { RequestMethod.GET,RequestMethod.POST })
	public String uploadPoster(Model model){
		model.addAttribute("custPoster", new CustPoster(CustPosterType.Recommend));
		return view(uploadPosterViewName);
	}
	
    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
	public @ResponseBody String delete(String key){
		if(custPosterService.deleteModel(key)){
			 return "success";
		}
		 return "error";
	}
    
    @RequestMapping(value = saveViewName,method = { RequestMethod.POST})
    public String save(Model model,CustPoster custPoster){
    	custPoster.setId(CommonUtils.getKeyId());
    	custPoster.setDeleteMark(YesOrNo.no);
    	custPoster.setCreateDate(DateUtils.getCurrent());
    	custPoster.setModelStatus(ModelStatus.add);
    	ActionResult actionResult;
    	if(custPosterService.applyChange(custPoster)){
    		actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        } 
    	model.addAttribute("custPoster", custPoster);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
    	return view(uploadPosterViewName);
    }
}
